package com.demo.mapper;

import com.demo.dto.ClassesDTO;
import com.demo.dto.ClassesPageQueryDTO;
import com.demo.pojo.Classes;
import com.demo.result.PageResult;
import com.demo.vo.ClassesVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface ClassesMapper {
    @Insert("INSERT INTO sys_classes(classID, className, depID, major, grade) " +
            "VALUES (#{classID}, #{className}, " +
            "(SELECT depID FROM sys_department WHERE depName = #{depName}), #{major}, #{grade})")
    void addClasses(Classes classes);


    @Delete("delete from sys_classes where classID=#{classID}")
    void delete(String classID);

    @Update("UPDATE sys_classes " +
            "SET depID = (SELECT depID FROM sys_department WHERE depName = #{depName}), " +
            "classID = #{classID}, className=#{className},major=#{major},grade=#{grade} " +
            "WHERE classID = #{classID}")
    void update(ClassesDTO classesDTO);


    @Select("<script>" +
            "SELECT c.*, sd.depName AS depName " +
            "FROM sys_classes c " +
            "LEFT JOIN sys_department sd ON c.depID = sd.depID " +
            "WHERE 1=1 " +
            "<if test='className != null and className.trim() != \"\"'> " +
            "AND c.className LIKE CONCAT('%', #{className}, '%') " +
            "</if> " +
            "<if test='classID != null and classID.trim() != \"\"'> " +
            "AND c.classID = #{classID} " +
            "</if> " +
            "ORDER BY c.classID " +
            "</script>")
    Page<ClassesVO> pageQuery(ClassesPageQueryDTO classesPageQueryDTO);

}
